package com.gbcom.autocompile.mail;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

public class MailSender {

	public static final Logger logger =Logger.getLogger(MailSender.class);
	
	private final Properties props;//仅构造函数可以赋值，其它只能读取，保证线程安全
	private final String MAIL_SENDER_NAME;
	private final String MAIL_SENDER_PASSWORD;
	private final String MAIL_SENDER_ADDR;
	private final Set<String>  MAIL_RECEIVER_SET ;
	
	private static final MailSender instance = new MailSender();
	
	private MailSender(){
		// 1.0 封装参数,从模板获取
		props = new Properties();
		props.put("mail.host", "smtp.163.com");//// 服务器地址  
		props.put("mail.smtp.port", "" + 25); // 端口号  
		props.put("mail.transport.protocol","smtp");// 暂时使用SMTP协议,可去掉
		props.put("mail.smtp.auth", "true");
		
		//自定义属性
		Set<String> receiver = new HashSet<String>();
		receiver.add("sunyanzheng@gbcom.com.cn");
		MAIL_SENDER_NAME = "assertmyself";
		MAIL_SENDER_PASSWORD = "sunday321";
		MAIL_SENDER_ADDR="assertmyself@163.com";
		MAIL_RECEIVER_SET = receiver;
	}
	
	/**
	 *  获取单例 
	 * @return MailSender
	 */
	public static MailSender getInstance(){
		return instance;
	}
	
	public void sendMail(String subject,String content) throws MessagingException{
		Message msg = buildDefaultMsg();
		msg.setSubject(subject);// 设定信中的主题
		msg.setSentDate(new Date());// 设定送信的时间
		msg.setContent(content, "text/html;charset=utf-8");
		msg.saveChanges();
		//4.0发送
		Transport.send(msg);
		
	}
	private Message buildDefaultMsg() throws MessagingException {
		// 2.0 产生新的邮件Session 服务
		SmtpAuthentic auth = new SmtpAuthentic(MAIL_SENDER_NAME, MAIL_SENDER_PASSWORD);
		Session mailSession = Session.getInstance(props, auth);//带简单鉴权
		mailSession.setDebug(true);////邮件打印
		
		// 3.0 封装邮件Msg
		Message msg = new MimeMessage(mailSession);
		msg.setFrom(new InternetAddress(MAIL_SENDER_ADDR));// 设定传送邮件的发信人
		InternetAddress address = null;
		for (String revAddress : MAIL_RECEIVER_SET) {
			address = new InternetAddress(revAddress);
			msg.addRecipient(Message.RecipientType.TO, address);
		}
		
		msg.setDataHandler(new javax.activation.DataHandler("alarm",
				"text/html"));
		return msg;
	}

	public void sendMail(String subject,String context,Set<String> files){
		
	}
	
	
	
	
	/**
	 * 发送邮件方法
	 * 
	 * 
	 * @param sendAddress
	 *            : 发件人地址
	 * @param name
	 *            : 发件人姓名
	 * @param password
	 *            : 发件人密码
	 * @param emailServer
	 *            : 发送的邮箱服务器地址 例smtp.163.com
	 * @param revAddresses
	 *            : 收件箱地址
	 * @param title
	 *            : 发送邮件的主题
	 * @param content
	 *            ： 发送邮件的内容 可以是超文本标记语言
	 * @throws MessagingException
	 *             MessagingException
	 * @throws AddressException
	 *             AddressException
	 * 
	 */
	public static void sendSmtpEmail(String sendAddress, String name,
			String password, String emailServer, String[] revAddresses,
			String title, String content) throws AddressException,
			MessagingException {
		if (revAddresses == null || revAddresses.length == 0) {
			return;
		}

		// 1.0 设置邮件协议属性
		Properties props = new Properties();
		props.put("mail.host", emailServer);//// 服务器地址  
		props.put("mail.smtp.port", "" + 25); // 端口号  
		props.put("mail.transport.protocol","smtp");// 暂时使用SMTP协议,可去掉
		props.put("mail.smtp.auth", "true");

		// 2.0 产生新的邮件Session 服务
		// Session mailSession = Session.getDefaultInstance(props, auth);//不带鉴权

		// 需鉴权,用户名及密码：：用户名为@前的部分,如"pujing@gbcom.com.cn",用户名为:pujing
		SmtpAuthentic auth = new SmtpAuthentic(name, password);
		Session mailSession = Session.getInstance(props, auth);//带简单鉴权
		mailSession.setDebug(true);////邮件打印
		
		// 3.0 封装邮件Msg
		Message msg = new MimeMessage(mailSession);
		msg.setFrom(new InternetAddress(sendAddress));// 设定传送邮件的发信人
		// 设定传送邮件至收信人的信箱列表
		InternetAddress address = null;
		for (String revAddress : revAddresses) {
			address = new InternetAddress(revAddress);
			msg.addRecipient(Message.RecipientType.TO, address);
		}

		
		msg.setSubject(title);// 设定信中的主题
		msg.setSentDate(new Date());// 设定送信的时间

		// 可以发送超文本的邮件 html格式 第一个参数可以指定为任意字符串
		// 设定传送信的MIME Type
		msg.setDataHandler(new javax.activation.DataHandler("alarm",
				"text/html"));

		// content为发送的内容
		msg.setContent(content, "text/html;charset=utf-8");
		msg.saveChanges();
		Transport.send(msg);
	}

	/**
	 * 发送邮件方法
	 * 
	 * @param sendAddress
	 *            : 发件人地址
	 * @param name
	 *            : 发件人姓名
	 * 
	 * 
	 * @param password
	 *            : 发件人密码
	 * 
	 * 
	 * @param emailServer
	 *            : 发送的邮箱服务器地址 例smtp.163.com
	 * @param revAddresses
	 *            : 收件箱地址
	 * @param title
	 *            : 发送邮件的主题
	 * @param content
	 *            ： 发送邮件的内容 可以是超文本标记语言
	 * @param file
	 *            ： 发送邮件的附件
	 * @throws MessagingException
	 *             MessagingException
	 * @throws AddressException
	 *             AddressException
	 * 
	 */
	public static void sendSmtpEmail(String sendAddress, String name,
			String password, String emailServer, String[] revAddresses,
			String title, String content, Vector<String> file)
			throws AddressException, MessagingException {
		if (revAddresses == null || revAddresses.length == 0) {
			return;
		}
		// 设定所要用的Mail 服务器和所使用的传输协议

		Properties props = new Properties();
		props.put("mail.host", emailServer);
		props.put("mail.smtp.auth", "true");
		SmtpAuthentic auth = new SmtpAuthentic(name, password);
		Session mailSession = Session.getInstance(props, auth);
		boolean sessionDebug = false;
		mailSession.setDebug(sessionDebug);
		Message msg = new MimeMessage(mailSession);
		msg.setFrom(new InternetAddress(sendAddress));
		InternetAddress address = null;
		for (String revAddress : revAddresses) {
			address = new InternetAddress(revAddress);
			msg.addRecipient(Message.RecipientType.TO, address);
		}
		msg.setSubject(title);
		msg.setSentDate(new Date());
		// msg.setDataHandler(new
		// javax.activation.DataHandler("alarm","text/html"));
		// multipart
		Multipart mp = new MimeMultipart();
		MimeBodyPart mbpContent = new MimeBodyPart();
		mbpContent.setText(content);
		mp.addBodyPart(mbpContent);

		/* 往邮件中添加附件 */
		Enumeration<String> efile = file.elements();
		String fileName;
		while (efile.hasMoreElements()) {
			MimeBodyPart mbpFile = new MimeBodyPart();
			fileName = efile.nextElement().toString();
			FileDataSource fds = new FileDataSource(fileName);
			mbpFile.setDataHandler(new DataHandler(fds));
			mbpFile.setFileName((fds.getName()));
			mp.addBodyPart(mbpFile);
		}
		msg.setContent(mp);

		msg.saveChanges();
		Transport.send(msg);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			for(int i=0;i<10;i++){
				MailSender.getInstance().sendMail("hellow", " 中国馆 ");
			}
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
	
}
